import Vue from 'vue'
import VueRouter from 'vue-router'

import Layout from '@/views/layout/index'
import Login from '@/views/login'
import Dashboard from '@/views/dashboard'
import Users from "@/views/user";
import Rights from "@/views/power/Rights";
import Roles from "@/views/power/Roles";
import Cate from "@/views/goods/Cate";
import Goods from "@/views/goods/Goods";
import Params from "@/views/goods/Params";
import OrdersList from "@/views/orders/OrdersList";
import DashBoard from "@/views/dashboard";
import store from "@/store";


Vue.use(VueRouter)

const routes = [

  { path: '/login', component: Login },
  { path: '/test', component: Goods },
  {
    path: '/',
    component: Layout,
    // 重定向
    redirect: '/users',
    children: [
      { path: 'dashboard', component: Dashboard },
      { path: "users", component: Users },
      { path: "rights", component: Rights },
      { path: "roles", component: Roles },
      { path: "categories", component: Cate },
      { path: "goods", component: Goods },
      { path: "params", component: Params },
      { path: "orders", component: OrdersList },
      { path: "reports", component: DashBoard },
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router



// 针对ElementUI导航栏中重复导航报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const whiteList = ['/login']
// to 将要访问的路径    from  从哪里来  next 放行 
router.beforeEach((to, from, next) => {
  const { token } = store.state.user.userInfo || {};
  if (token) {
    next()
    return
  }
  if (whiteList.includes(to.path)) {
    next()
    return
  }
  next('/login')
})
